<?php
	/* 此类主要是做CURD的方法测试
	 * @me 
	 * @date
	 */
	class UserAction extends Action{
		//插入语句add()
		public function add(){
			header('content-Type:text/thml;charset=utf-8');
			$Dao = M('User');
			$usad = array('username'=>'hoho','age'=>27,'password'=>'520');
			if($lastInsId = $Dao->add($usad)){
				  echo "插入数据 id 为：$lastInsId";
			} 
			else {
				  $this->error('数据写入错误！');
			}
		}
        //查询语句 select()
		public function read(){
		 	header('content-Type:text/html;charset=utf-8');
			$Gets = M('User');
			$listShow = $Gets->select();
			//dump($listShow);
			/*用循环方法
			 * 查询出来的
			foreach($listShow as $dvd){
				
				foreach ($dvd as $key=>$value) {
					echo $key."->".$value.PHP_EOL;
				}
				
			}
			*/
			//下面的用是的模板方式
			 $this->assign("list",$listShow);
			 $this->display();
		}
		
		//查询find取得字段，此方式取得是一条数据
		public function checkUser(){
			header('content-Type:text/html;charset=utf-8');
			$Dao = M('User');
			$condition['username'] = 'job';
			$condition['password'] = 'afei123';
			$list = $Dao->where($condition)->find();
			if($list){
				echo '查询成功!';
			}	
			else{
				echo '查询失败!';
			}
		}
		
		//查询getBy字段()注意此方法后跟字段首字母要大写
		public function getByuser(){
			header('content-Type:text/html;charset=utf-8');
			$Dao = M('User');
			$user = $Dao->getByUsername('job');
			if($user){
				echo '此用户存在!';
			}
			else{
				echo '此用户不存在!';
			}
		}
		
		//查询getField()，后面不用分隔符连接是返回是一个二维数组，跟连接符是一维数组
		public function getFi(){
			header('content-Type:text/html;charset=utf-8');
			$Dao = M('User');
			$getlist = $Dao->where('uid<5')->getField('uid,username,password',':');
			dump($getlist);
				
		}
		
		//用切换swithchModel方式去使用getN定位游标
		public function getNum(){
			header('content-Type:text/html;charset=utf-8');
			$Dao = M('User');
			$getNList = $Dao->switchModel('Adv')->where('uid>3')->order('uid ASC')->getN(2);	
			dump($getNList);
			
		}
		
		//用Model方式新建一个空的AdvModel的UserModel方法,然后测试first和last
		public function getNAdv(){
			$Dao = D('User');
			$list = $Dao->where('uid > 5')->order('uid ASC')->last();
			dump($list);
		}
		
		//使用原生态的sql语句,请用__TABLE__来代替原表名ts_user
		public function querySQL(){
			header('Content-Type:text/html;charset=utf-8');
			$Dao = M('User');
			//dump(__TABLE__);exit();
			$listsql = $Dao->query('select * from __TABLE__ where uid>10');
			if($listsql){
				dump($listsql);
			}
			else{
				echo '<h1>'.'没有找到相关内容!'.'</h1>';
			}
		}
		
		
		//where() 最好用数组的方式做成条件比较安全些
		public function whis(){
			header('Content-Type:text/html;charset=utf-8');
			$Dao = M('User');
			$condition['uid'] = array('gt',1);
			$condition['username'] = 'job'; 
			$whstr = $Dao->where($condition)->select();
			if($whstr){
				dump($whstr);
			}	
		   else{
			    echo '查询的结果不存在!';
		    }
		}
		
		//save() 更新数据库相当于update
		public function userSave(){
			header('Content-Type:text/html;charset=utf-8');
			$Dao = M('User');
			$condition['username'] = 'hoho';
			$data['age'] = 23;
			$reslist = $Dao->where($condition)->save($data); 
			if($reslist !== false){
				echo '更新成功';
			}	
			else{
				echo '更新失败';
			}
			
		}
		
		//delete删除数据
		
		public function delUser(){
			header('Content-Type:text/html;charset=utf-8');				
			$Dao = M('User');
			$condition['uid'] = 12;
			$Dao->where($condition)->delete();
			if($Dao!==false){
				echo "删除成功!";
			}
			else{
				echo "删除失败!";
			}
		}
		
		public function setfields(){
			header('Content-Type:text/html;charset=utf-8');
			$Dao = M('User');
			$data['username']='afei';
			$setlist = $Dao->where('uid=7')->setField($data);	
			if($setlist!==false){
				echo '修改成功';
			}
			else{
				echo '修改失败';
			}
		}
		
	}
?>